define(["jquery", "easy-admin"], function ($, ea) {

    var form = layui.form,
        tableSelect = layui.tableSelect;

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'admin/channelActivity/index?channel_id=' + window.channelId,
        add_url: 'admin/channelActivity/add?channel_id=' + window.channelId,
        edit_url: 'admin/channelActivity/edit?channel_id=' + window.channelId,
        delete_url: 'admin/channelActivity/delete?channel_id=' + window.channelId,
        export_url: 'admin/channelActivity/export?channel_id=' + window.channelId,
        modify_url: 'admin/channelActivity/modify?channel_id=' + window.channelId,
    };

    var Controller = {

        index: function () {
            ea.table.render({
                init: init,
                toolbar: ['refresh', 'add'],
                cols: [[
                    {field: 'activity_title', title: '活动名称', search: false},
                    {width: 250, title: '操作', templet: ea.table.tool, operat: ['delete']},
                ]],
            });

            ea.listen();
        },
        add: function () {
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
    };

    // 选择活动
    var activitySelectList = document.querySelectorAll("[data-activity-select]");
    if (activitySelectList.length > 0) {
        $.each(activitySelectList, function (i, v) {

            var selectName = $(this).attr('data-activity-select'),
                selectNumber = $(this).attr('data-activity-select-number') || 'one',
                selectElem = $(this).attr('id'),
                selectCheck = selectNumber === 'one' ? 'radio' : 'checkbox',
                selectUrl = $(this).attr('data-activity-select-url');

            var listId = $(this).attr('data-activity-select_id') || 'id',
                listName = $(this).attr('data-activity-select_name') || '';

            var elem = "input[name='" + selectName + "']";

            tableSelect.render({
                elem: "#" + selectElem,
                checkedKey: 'id',
                searchType: 'more',
                searchList: [
                    {searchKey: 'name', searchPlaceholder: '请输入商户名称'},
                ],
                table: {
                    url: selectUrl,
                    cols: [[
                        {type: selectCheck},
                        {field: 'activity_title', title: '活动名称', align: "center"},
                    ]]
                },
                done: function (e, data) {
                    var array = [];
                    $.each(data.data, function (index, val) {
                        array.push("[" + val[listId] + "] " + val[listName])
                    });
                    ea.msg.success('选择成功', function () {
                        $(elem).val(array.join(', '));
                        $(elem).trigger("input");
                    });
                }
            })
        });
    }

    // 初始化layui表单
    form.render();

    return Controller;
});